package com.erb.controller.llb;

import com.erb.constants.AdminConstants;
import com.erb.constants.NoticeConstants;
import com.erb.entity.Advertisement;
import com.erb.entity.Notice;
import com.erb.entity.Pager;
import com.erb.entity.RespBean;
import com.erb.service.llb.IAdvertisementService;
import com.erb.service.llb.INoticeService;
import com.erb.service.llb.IndexService;
import com.erb.utils.file.DeleteFile;
import com.erb.utils.file.FileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 广告管理后台控制器
 *
 * @author 南城
 */

@Controller
@RequestMapping("/admin/advertisement")
public class AdvertisementController {

	@Autowired
	IndexService indexService;
	@Autowired
	IAdvertisementService advertisementService;
	@Autowired
	INoticeService noticeService;

	/**
	 * 轮播图列表
	 */
	@GetMapping("/adver-list")
	public String adverList() {
		return "admin/advertisement/adver-list";
	}

	/**
	 * 公告列表
	 */
	@GetMapping("/notice-list")
	public String noticeList() {
		return "admin/advertisement/notice-list";
	}

	/**
	 * 获取轮播图
	 */
	@GetMapping("/findAdvertisement")
	@ResponseBody
	public RespBean findAdvertisement() {
		List<Advertisement> advertisements = indexService.findAdvertisement(null);
		return RespBean.ok("查询成功", advertisements);
	}

	/**
	 * 获取轮播图
	 *
	 * @param id
	 * @return
	 */
	@GetMapping("/findAdvertisementById/{id}")
	@ResponseBody
	public RespBean findAdvertisementById(@PathVariable int id) {
		Advertisement advertisement = advertisementService.findAdvertisementById(id);
		return RespBean.ok("查询成功", advertisement);
	}

	/**
	 * 更新轮播图状态
	 *
	 * @param advertisement
	 * @return
	 */
	@PutMapping("/updateAdvertisementStatus")
	@ResponseBody
	public RespBean updateAdvertisementStatus(@RequestBody Advertisement advertisement) {
		System.out.println(advertisement);
		if (advertisementService.updateAdvertisementStatus((int) advertisement.getId(), advertisement.getIsShow()) > 0) {
			return RespBean.ok("修改成功");
		} else {
			return RespBean.error("修改失败");
		}
	}

	/**
	 * 添加轮播图
	 *
	 * @param advertisement
	 * @param time
	 * @param img
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/addAdvertisement")
	@ResponseBody
	public RespBean addAdvertisement(Advertisement advertisement, String time, MultipartFile img) throws Exception {
		String fileName = FileUpload.uploadAndRename(AdminConstants.ADVERTISEMENT_SAVE_PATH, img);
		advertisement.setImgUrl(fileName);
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if ("".equals(time) || null == time) {
			advertisement.setEndTime(new Timestamp(simpleDateFormat.parse("2038-01-01 00:00:00").getTime()));
		} else {

			try {
				Date parse = simpleDateFormat.parse(time);
				Timestamp timestamp = new Timestamp(parse.getTime());
				advertisement.setEndTime(timestamp);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		if (advertisementService.addAdvertisement(advertisement) > 0) {
			return RespBean.ok("添加成功");
		} else {
			return RespBean.error("添加失败");
		}
	}

	/**
	 * 修改轮播图
	 *
	 * @param advertisement
	 * @param time
	 * @param img
	 * @return
	 * @throws Exception
	 */
	@PutMapping("/updateAdvertisement")
	@ResponseBody
	public RespBean updateAdvertisement(Advertisement advertisement, String time, MultipartFile img) throws Exception {
		if (img != null) {
			//删除原来的图片
			DeleteFile.deleteFile(AdminConstants.ADVERTISEMENT_SAVE_PATH + advertisement.getImgUrl());
			String fileName = FileUpload.uploadAndRename(AdminConstants.ADVERTISEMENT_SAVE_PATH, img);
			advertisement.setImgUrl(fileName);
		}
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if ("".equals(time) || null == time) {
			advertisement.setEndTime(new Timestamp(simpleDateFormat.parse("2038-01-01 00:00:00").getTime()));
		} else {
			Date parse = simpleDateFormat.parse(time);
			Timestamp timestamp = new Timestamp(parse.getTime());
			advertisement.setEndTime(timestamp);
		}
		System.out.println(advertisement);
		if (advertisementService.updateAdvertisement(advertisement) > 0) {
			return RespBean.ok("修改成功");
		} else {
			return RespBean.error("修改失败");
		}
	}

	/**
	 * 删除轮播图
	 */
	@DeleteMapping("/deleteAdvertisement/{id}")
	@ResponseBody
	public RespBean deleteAdvertisement(@PathVariable int id) {
		if (advertisementService.deleteAdvertisement(id) > 0) {
			return RespBean.ok("删除成功");
		} else {
			return RespBean.error("删除失败");
		}
	}

	//获取公告
	@GetMapping("/findNoticeWithPage")
	@ResponseBody
	public Pager findNoticeWithPage(Pager pager) {
		pager.page(indexService.countNotice());
		List<Notice> notices = indexService.findNoticeWithPage(pager);
		pager.data = notices;
		return pager;
	}

	/**
	 * 获取公告
	 *
	 * @param id
	 * @return
	 */
	@GetMapping("/findNoticeById/{id}")
	@ResponseBody
	public RespBean findNoticeById(@PathVariable int id) {
		Notice notice = noticeService.findNoticeById(id);
		return RespBean.ok("查询成功", notice);
	}

	/**
	 * 获取公告分类
	 *
	 * @return
	 */
	@GetMapping("/findNoticeType")
	@ResponseBody
	public RespBean findNoticeType() {
		return RespBean.ok("查询成功", NoticeConstants.NOTICE_TYPE_MAP);
	}

	/**
	 * 添加公告
	 *
	 * @param notice
	 * @return
	 */
	@PostMapping("/addNotice")
	@ResponseBody
	public RespBean addNotice(@RequestBody Notice notice) {
		notice.setCreateDate(new Timestamp(System.currentTimeMillis()));
		if (noticeService.addNotice(notice) > 0) {
			return RespBean.ok("添加成功");
		} else {
			return RespBean.error("添加失败");
		}
	}

	/**
	 * 修改公告
	 *
	 * @param notice
	 * @return
	 */
	@PutMapping("/updateNotice")
	@ResponseBody
	public RespBean updateNotice(@RequestBody Notice notice) {
		if (noticeService.updateNotice(notice) > 0) {
			return RespBean.ok("修改成功");
		} else {
			return RespBean.error("修改失败");
		}
	}

	/**
	 * 删除轮播图
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping("/deleteNotice/{id}")
	@ResponseBody
	public RespBean deleteNotice(@PathVariable int id) {
		if (noticeService.deleteNotice(id) > 0) {
			return RespBean.ok("删除成功");
		} else {
			return RespBean.error("删除失败");
		}
	}


}
